/*实用工具类的实现
  1.获取系统时间
  2.判断文件是否存在
  3.获取文件所在路径
  4.创建目录
*/
#ifndef _UT_IL_H
#define _UT_IL_H
#include<iostream>
#include<ctime>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
namespace tqblog{
    namespace util{
       class Date{
         public:
            static time_t now(){
                return (time_t)time(nullptr);
            }
        };
        class File{
            public:
            static bool exists(const std::string &pathname){
                  struct stat st;
                  return stat(pathname.c_str(),&st)==0;  
            };
            static std::string path(const std::string &pathname){
                int pos=pathname.find_last_of("/\\");
                if(pos==std::string::npos)return "./";
                return pathname.substr(0,pos+1);
            };
            static void createDirectory(const std::string &pathname){
                if(pathname.empty())return;
                int index=0;
                while(index<pathname.size()){
                    int pos=pathname.find_first_of("/\\",index);
                    if(pos==std::string::npos){
                        mkdir(pathname.c_str(),0777);
                        return;
                    }
                    std::string parent=pathname.substr(0,pos+1);
                    if(exists(parent)){index=pos+1;continue;}
                    mkdir(parent.c_str(),0777);
                    index=pos+1;
                }

            };

        };
       
    };
};
#endif